package com.pubinfo.passbook.engine.process.constant;

import com.pubinfo.passbook.common.entity.TEnginePlaceholderConst;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.service.engine.EngineComputePlaceHolderConstService;
import com.pubinfo.passbook.engine.constants.ComputeParamType;
import com.pubinfo.passbook.common.utils.ApplicationContextUilts;
import com.pubinfo.passbook.engine.constants.ComputeChainType;
import com.pubinfo.passbook.engine.constants.ComputeResultType;
import com.pubinfo.passbook.engine.process.ParamenterProcessor;

import java.math.BigDecimal;

/**
 * 常量处理器 (所有常量共用这一个处理器)
 *
 * @author zhouch
 * @date 2021/12/16 11:18
 */
public class ConstantProcessor extends ParamenterProcessor<Object, BigDecimal> {

    EngineComputePlaceHolderConstService constService;

    private Integer id;

    @Override
    public BigDecimal get(Object _) {
        TEnginePlaceholderConst constant = constService.getById(id);
        if (constant == null) {
            throw new ServiceException("常量占位符不存在 ID：" + id);
        }
        return constant.getValue();
    }

    public ConstantProcessor(Integer constantId) {
        id = constantId;
        constService = ApplicationContextUilts.getBean(EngineComputePlaceHolderConstService.class);
    }

    @Override
    public String getResultType() {
        return ComputeResultType.NUMBER;
    }

    @Override
    public String getChainType() {
        return ComputeChainType.COMMON;
    }

    @Override
    public String getType() {
        return ComputeParamType.CONSTANT;
    }

}
